package com.chuangyang.fixboxmaster.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.BaseInfo;
import com.chuangyang.fixboxmaster.bean.CashInfo;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by vanshu on 15/6/6 14:06.
 */
public class CashRequestActivity extends BaseActivity implements View.OnClickListener, TextView.OnEditorActionListener {

    private static final int VIEW_ID_CASH_INPUT = 1;
    private static final int VIEW_ID_CASH_CONFIRM = 2;
    private int mCurrentViewID = VIEW_ID_CASH_INPUT;
    private TextView mCardNum;// 绑定卡号
    private TextView mCardAddress;// 开户行
    private TextView mTotalCash;// 账户余额
    private TextView mUsableCash;// 可提现余额
    private EditText mRequestCashInput;// 申请提现金额
    private TextView mRequestCashTitle;
    private TextView mAccountName;// 姓名
    private TextView mRequestCash;//提款金额
    private TextView mBalanceCash;// 提现后剩余金额
    private TextView mReceiveDate;// 到帐日
    private Button mConfirmBtn;
    private LinearLayout mCashInputView;
    private LinearLayout mCashConfirmView;
    private CashInfo cashInfo = new CashInfo();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getResources().getString(R.string.title_activity_cash_request));
        setContentView(R.layout.fragment_cash_request);
        cashInfo = (CashInfo) getIntent().getSerializableExtra("cashInfo");

        mCardNum = (TextView) findViewById(R.id.bank_card_num);
        mCardAddress = (TextView) findViewById(R.id.bank_card_address);
        mTotalCash = (TextView) findViewById(R.id.total_cash);
        mUsableCash = (TextView) findViewById(R.id.usable_cash);
        mCashInputView = (LinearLayout) findViewById(R.id.request_cash_input_view);
        mRequestCashTitle = (TextView) findViewById(R.id.request_cash_title);
        mRequestCashInput = (EditText) findViewById(R.id.request_cash_input);
        mCashConfirmView = (LinearLayout) findViewById(R.id.request_cash_confirm_view);
        mAccountName = (TextView) findViewById(R.id.account_name);
        mRequestCash = (TextView) findViewById(R.id.request_cash_confirm);
        mBalanceCash = (TextView) findViewById(R.id.balance_cash);
        mReceiveDate = (TextView) findViewById(R.id.receive_date);
        mConfirmBtn = (Button) findViewById(R.id.confirm);
        mRequestCashInput.setOnEditorActionListener(this);
        mConfirmBtn.setOnClickListener(this);
        mCardNum.setText(cashInfo.cardNum); //银行卡号
        mCardAddress.setText(cashInfo.cardAddress);
        mTotalCash.setText("￥" + String.valueOf(cashInfo.balance)); //余额
        mUsableCash.setText("￥" + String.valueOf(cashInfo.cash)); //可用余额
        mAccountName.setText(cashInfo.accountName); //姓名
        mBalanceCash.setText("100");
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd号");
        mReceiveDate.setText(formatter.format(new Date()) + "24:00之前");
        showView(VIEW_ID_CASH_INPUT);

    }

    private void showView(int viewID) {
        mCurrentViewID = VIEW_ID_CASH_INPUT;
        mCashInputView.setVisibility(View.GONE);
        mCashConfirmView.setVisibility(View.GONE);
        if (viewID == VIEW_ID_CASH_INPUT) {
            mCashInputView.setVisibility(View.VISIBLE);
            mCashConfirmView.setVisibility(View.GONE);
            mRequestCashTitle.setText("提现金额");
            mConfirmBtn.setText("下一步");
        } else if (viewID == VIEW_ID_CASH_CONFIRM) {
            mCurrentViewID = VIEW_ID_CASH_CONFIRM;
            cashInfo.requestCash = mRequestCashInput.getText().toString().trim();
            cashInfo.remainCash = "" + (Double.parseDouble(cashInfo.cash) - Double.parseDouble(cashInfo.requestCash));
            mCashInputView.setVisibility(View.GONE);
            mCashConfirmView.setVisibility(View.VISIBLE);
            mRequestCashTitle.setText("提现信息");
            mConfirmBtn.setText("确认提款");
            mRequestCash.setText("￥" + cashInfo.requestCash);
            mBalanceCash.setText("￥" + cashInfo.remainCash);
        }
    }

    private boolean validateCash() {
        cashInfo.requestCash = mRequestCashInput.getText().toString().trim();
        if (TextUtils.isEmpty(cashInfo.requestCash)) {
            mRequestCashInput.setError("金额不能为空!");
            return false;
        } else if (cashInfo.requestCash.length() > 9) {
            mRequestCashInput.setError("输入金额过大,请重新输入!");
            return false;
        } else {
            double nRequestCash = Double.parseDouble(cashInfo.requestCash);
            double nUsableCash = Double.parseDouble(cashInfo.cash);
            if (nRequestCash == 0) {
                mRequestCashInput.setError("金额不能为零!");
                return false;
            } else if (nRequestCash % 100 != 0) {
                mRequestCashInput.setError("提款金额必须为100的整数倍!");
                return false;
            } else if (nRequestCash > nUsableCash) {
                mRequestCashInput.setError("金额不能超过可提现金额!");
                return false;
            }
        }
        return true;
    }


    private void confirmBtnClick() {
        if (mCurrentViewID == VIEW_ID_CASH_INPUT) {
            if (validateCash()) {
                showView(VIEW_ID_CASH_CONFIRM);
            }
        } else if (mCurrentViewID == VIEW_ID_CASH_CONFIRM) {
            submit();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                confirmBtnClick();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (v == mRequestCashInput) {
            switch (actionId) {
                case EditorInfo.IME_ACTION_DONE:
                    confirmBtnClick();
                    return true;
            }
        }
        return false;
    }


    private void submit() {
        HashMap<String, String> param = new HashMap<>();
        param.put("withdrawCashAmount", cashInfo.requestCash);
        param.put("bankCardNo", cashInfo.cardNum);
        addRequest(new GsonRequest<>(FixBoxApi.CASH_REQUEST, param, BaseInfo.class, responseListener));
    }


    private Response.Listener<BaseInfo> responseListener = new Response.Listener<BaseInfo>() {
        @Override
        public void onResponse(BaseInfo response) {
            Toast.makeText(CashRequestActivity.this, "提现成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    };
}
